package example.controller;

import example.pojo.user;
import example.service.login_check;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@Controller
@Resource
public class login {

    @RequestMapping(value="/login",produces = "text/html;charset=UTF-8")
    @ResponseBody
    protected String doPost(HttpServletRequest request, HttpServletResponse response , HttpSession session) throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=utf-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //1.把账号、密码分别设置为root、123
        if(login_check.check(username , password)){
            //2.创建User对象保存账号、密码
            user user=new user();
            user.setUsername(username);
            user.setPassword(password);
            //3.创建Session对象保存User对象
            session.setAttribute("username", user);
            session.setMaxInactiveInterval(1000);
            //4.登陆成功跳转指定页面
            return "1";
        }
        else{
            return "2";
        }
    }

    @RequestMapping(value="/session-d",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String session(HttpServletRequest request)  {
        {
            HttpSession uuu = request.getSession();
            uuu.removeAttribute("username");
            user user1 = (user) uuu.getAttribute("username");
            if ( user1 == null )
            {
                return "1";
            }
            return "2";
        }
    }


}


